package cn.fengso.service.impl;

import java.util.Hashtable;
import java.util.Map;
import java.util.Random;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException; 
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import org.quartz.impl.StdSchedulerFactory;

import cn.fengso.config.Config;
import cn.fengso.job.CountJob;
import cn.fengso.service.IMessageHandlerService;
import cn.fengso.vo.Message;

public class MessageHandlerServiceImpl implements IMessageHandlerService{
 
	public static Map<String,Integer> countMap =new Hashtable<String, Integer>(); 
	public static Map<String,Integer> fixedCountMap =new Hashtable<String, Integer>(); 
	
	@SuppressWarnings("static-access")
	public MessageHandlerServiceImpl(){
		try {
			Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
		    JobDetail jobDetail = new JobDetail("countJOB", scheduler.DEFAULT_GROUP, CountJob.class);  
		    Trigger trigger = TriggerUtils.makeSecondlyTrigger(Config.getInteger("INTVAL"));   
		    trigger.setName("handlerMsg");
		    scheduler.scheduleJob(jobDetail, trigger);  
		    scheduler.start(); 
		} catch (SchedulerException e) { 
			e.printStackTrace();
		}   
	}
	public void receive(Message msg) { 
		if(msg.getLevel()<Config.getInteger("LEVEL")){
			return;
		}
		if(!countMap.containsKey(msg.getArea())){
			countMap.put(msg.getArea(), 1);
		}else{
			countMap.put(msg.getArea(), countMap.get(msg.getArea())+1);
		}
	}
	
	public static void main(String[] s){ 
		final MessageHandlerServiceImpl i=new MessageHandlerServiceImpl();
		new Thread(new Runnable(){

			public void run() {
				Random rd=new Random();
				while(true){
					//countMap.put("成都", rd.nextInt(100));
					Message msg=new Message();
					msg.setArea("成都");
					msg.setLevel(5);
					i.receive(msg);
			 
					
					try {
						Thread.sleep(rd.nextInt(150)); 
					} catch (InterruptedException e) { 
						e.printStackTrace();
					}
				} 
			}}).start();
		
		new Thread(new Runnable(){

			public void run() {
				Random rd=new Random();
				while(true){
			 
					Message msg1=new Message();
					msg1.setArea("南充");
					msg1.setLevel(5);
					i.receive(msg1);
					
					try {
						Thread.sleep(rd.nextInt(250)); 
					} catch (InterruptedException e) { 
						e.printStackTrace();
					}
				} 
			}}).start();
	}

}
